package com.hellojavaweb.servlet;

import com.hellojavaweb.dao.UserDao;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

/**
 * Created by daitiantian on 2017/1/12.
 */
public class RegisterServlet_dtt extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {


        //接收用户名和密码
        String username = null;
        String pass = null;

        username = request.getParameter("username");
        pass = request.getParameter("pass1");


        UserDao userDao = new UserDao();


        boolean b = false;
        try {
            b=userDao.sleDataBase(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (b){
            //insert
            int rs1 = 0;
            try {
                rs1 = userDao.insDataBase(username,pass);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (rs1 == 1) {
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                response.sendRedirect("main_dtt.jsp");
            }
            }
            else
            {
                ServletContext application = request.getServletContext();
                application.setAttribute("message", "用户名重复");
                response.sendRedirect("err_dtt.jsp");

            }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request, response);
    }
}


